package com.androidRtc.controller;

import com.androidRtc.dao.UserDao;
import com.androidRtc.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UpdatePwdServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username,password_new,password_old;
        UserDao dao = new UserDao();
        PrintWriter out = response.getWriter();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        int result_select = 0;
        int result_update = 0;
        //1.调用请求对象对请求体使用utf-8字符集进行重新编辑
        request.setCharacterEncoding("utf-8");
        //2.调用请求对象读取请求体参数信息
        username = request.getParameter("username");
        password_old = request.getParameter("password_old");
        password_new = request.getParameter("password_new");
        //3.调用Dao将查询验证信息推送到数据库服务器上
        result_select = dao.login(username,password_old);
        //4.调用响应对象，根据验证结果将不同资源文件地址写入到响应头，交给浏览器
        if(result_select == 1){
            result_update = dao.update(username,password_new);
        }else if(result_select == 0){
            out.print(0);
            out.close();
        }
        if(result_select == 1 && result_update == 1){
            out.print(1);
            out.close();
        }else if(result_select == 1 & result_update ==0){
            out.print(2);
            out.close();
        }

    }
}
